package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.page(categoryPageQueryDTO);
        long total = page.getTotal();
        List<Category> result = page.getResult();
        return new PageResult(total, result);
    }

    /**
     * 修改
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
//        categoryDTO.setUpdateTime(LocalDateTime.now());
//        categoryDTO.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(categoryDTO);
    }

    /**
     * 启用/禁用分类
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        categoryMapper.startOrStop(status, id);
    }

    /**
     * 新增菜品/套餐
     * @param categoryDTO
     */
    @Override
    public void add(CategoryDTO categoryDTO) {
//        categoryDTO.setCreateTime(LocalDateTime.now());
//        categoryDTO.setUpdateTime(LocalDateTime.now());
//        Long currentId = BaseContext.getCurrentId();
//        categoryDTO.setCreateUser(currentId);
//        categoryDTO.setUpdateUser(currentId);
        categoryDTO.setStatus(1);
        categoryMapper.add(categoryDTO);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        Integer cnt1 = dishMapper.cntById(id);
        Integer cnt2 = setmealMapper.cntById(id);
        if (cnt1 > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        if (cnt2 > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }

    /**
     * 条件查询
     * @return
     */
    @Override
    public List<Category> query() {
        return categoryMapper.query();
    }
}
